<?php
/**
 * Created by PhpStorm.
 * User: piyechao
 * Date: 2017/9/5
 * Time: 13:31
 */
class EntityCardService extends BaseService
{
    private $mCardSet;
    private $mCard;
    private $mCardBusinessRecord;
    private $mCardUser;
    private $mCardStatistics;
    private $mCardOrder;
    public  $accountId;
//    public function __construct()
//    {
//        $this->_initialize();
//    }
    public function _initialize()
    {
        $this->mUser = D('User');
        $this->mCard = D('ValueCard');
        $this->mCardSet = D('ValueCardSet');
        $this->mCardBusinessRecord = D('ValueCardBusinessRecord');
        $this->mCardUser = D('ValueCardUser');
        $this->mCardStatistics = D('ValueCardStatistics');
        $this->mCardOrder = D('ValueCardOrder');

    }

    /**
     * 获取模块id
     * @param $accountId 主账号id
     * @return mixed
     */
    public function getFunctionId($accountId){

        $cardSet = $this->mCardSet->where(array('account_id'=>$accountId,'cut_off'=>0))->find();

        if (!$cardSet){
            $data = array(
                'account_id'=>$accountId,
                'cut_off'=>0
            );
            $res= $this->mCardSet->add($data);
            if ($res){
                return $res['vc_id'];
            }else{
                throw_exception('获取模块id失败',12000);
            }
        }else{
            return $cardSet['vc_id'];
        }
    }

    /**
     * 获取模块信息
     * @param $vcId
     * @param $accountId
     * @return mixed
     */
    public function getSettingInfo($vcId,$accountId){

        $res = $this->mCardSet->where(array('vc_id'=>$vcId,'account_id'=>$accountId,'cut_off'=>0))->limit(1)->find();

        return $res;

    }


    /**
     * 向交易记录表添加数据
     * @param $data
     * @return mixed
     */
    public function addBusinessRecord($vcId,$order = array()){
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--$order:'.var_export($order ,true).'----'.$vcId ,'Utilities');
        $condition = array('user_id'=>$order['user_id'] ,'vc_id'=>$vcId);
        $userName = $this->mCardUser->where($condition)->getField('u_name');
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--mCardUser.sql:'.$this->mCardUser->_sql() ,'Utilities');
        $card = $this->getCardFieldByPk($order['card_id']);
//wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--$card:'.var_export($card ,true) ,'Utilities');
        //更新余额
        $cardMoney = $card['card_money'] + $order['order_amount'];
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--$cardMoney:'.$cardMoney ,'Utilities');
        $parm = array('card_id'=>$card['card_id'] ,'card_money'=>$cardMoney);
        $re = $this->mCard->save($parm);
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--$card.sql:'.$this->mCard->_sql() ,'Utilities');
        $data = array(
            'vc_id'=>$vcId,
            'order_sn'=>$order['order_sn'],
            'card_id'=>$order['card_id'],
            'u_id'=>$card['u_id'],
            'type'=>3,
            'amount'=>$order['order_amount'],
            'balance'=>$cardMoney,
            'create_time'=>time(),
            'card_number'=>$card['card_number'],
            'oprator_id'=>$order['user_id'],
            'operator_name'=>$userName,
        );
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--$data:'.var_export($data ,true) ,'Utilities');
        $res = $this->mCardBusinessRecord->add($data);
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--cmCardBusinessRecord_sql:'.$this->mCardBusinessRecord->_sql() ,'Utilities');
        return $re && $res;
    }

    /**
     * 添加或更新统计记录
     * @param $vcId
     * @param $amount 金额
     * @param int $type 1:充值,2:退款
     * @return int
     */
    public function addStatistics($vcId,$amount){
        // 获取年月日的时间戳
        $today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        //查询模块下当前时间有无记录
        $where = array(
            'vc_id'=>$vcId,
            'statistics_time'=>$today,
            'pay_type'=>1
        );
        $res = $this->mCardStatistics->where($where)->limit(1)->find();
//        wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--mCardStatistics_sql:'.$this->mCardStatistics->_sql() ,'Utilities');
        if (!$res){
            // 没有,就创建记录
            $data = array(
                'vc_id'=>$vcId,
                'statistics_time'=>$today,
                'recharge_times'=>1,
                'recharge_amount'=>$amount,
                'refund_times'=>0,
                'refund_amount'=>0,
                'profit_amount'=>$amount,
                'update_time'=>date('Y-m-d H:i:s',time()),
                'pay_type'=>1
            );
            $s = $this->mCardStatistics->add($data);
//            wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--mCardStatistics_sql:'.$this->mCardStatistics->_sql() ,'Utilities');
        }else{
            // 更新记录
            $data = array(
                'recharge_times'=>$res['recharge_times']+1,
                'recharge_amount'=>$res['recharge_amount']+$amount,
                'profit_amount'=>$res['profit_amount']+$amount,
                'update_time'=>date('Y-m-d H:i:s',time())
            );
            $s = $this->mCardStatistics->where($where)->save($data);
//            wLog(date('Y-m-d H:i:s').'--'.__LINE__.'行--mCardStatistics_sql:'.$this->mCardStatistics->_sql() ,'Utilities');
        }
        return $s;
    }

    /**
     * 更新卡信息
     * @param $condition
     * @param $data
     * @return mixed
     */
    public function updateCardInfo($condition = array() ,$data = array()){

        $res = $this->mCard->where($condition)->save($data);

        return $res;
    }

    /**
     * 更新用户信息
     * @param $data
     * @return mixed
     * @Auth    : piyechao
     * @Now     :2017/9/6 17:51
     */
    public function updateCardUserInfo($where = array() ,$data = array())
    {
        $user = $this->mCardUser->where($where)->find();
        if($user){
            $res = $this->mCardUser->where($where)->save($data);
            return $user['u_id'];
        }else{
            $res = $this->mCardUser->add($data);
            return $res;
        }

    }

    /**
     * 实体卡用户通过主键获得字段
     * @param $pk
     * @param string $field
     * @return mixed
     * @Auth    : piyechao
     * @Now     :2017/9/7 9:32
     */
    public function getCardUserFieldByPk($pk)
    {
        $re = $this->mCardUser->where(array('u_id'=>$pk))->find();
        return $re;
    }

    /**
     * 实体卡通过主键获得字段
     * @param $pk
     * @param string $field
     * @return mixed
     * @Auth    : piyechao
     * @Now     :2017/9/7 18:43
     */
    public function getCardFieldByPk($pk)
    {
        $re = $this->mCard->where(array('card_id'=>$pk))->find();
        return $re;
    }

    /**
     * 订单表通过主键获得字段
     * @param $pk
     * @param string $field
     * @return mixed
     * @Auth    : piyechao
     * @Now     :2017/9/7 18:47
     */
    public function getCardOrderFieldByPk($pk ,$field = '')
    {
        $order = $this->mCardOrder->where(array('order_id'=>$pk))->find();
        return $order;
    }

    /**
     * 通过条件获得当前实体卡用户信息
     * @param $condition
     * @return mixed
     * @Auth    : piyechao
     * @Now     :2017/9/6 16:49
     */
    public function getCardUserInfoByCondition($condition,$field='')
    {
        $res = $this->mCardUser->field($field)->where($condition)->limit(1)->find();
        return $res;
    }

    /**
     * 通过条件获得当前实体卡信息
     * @param $codition
     * @Auth    : piyechao
     * @Now     :2017/9/6 16:47
     */
    public function getCardInfoByCondition($condition,$field='')
    {
        $card = $this->mCard->field($field)->where($condition)->limit(1)->find();
        return $card;
    }

}